package com.ljtxxhwj.myblob.controller;


import com.ljtxxhwj.myblob.entity.Article;
import com.ljtxxhwj.myblob.entity.ArticleKind;
import com.ljtxxhwj.myblob.entity.ArticleTag;
import com.ljtxxhwj.myblob.entity.DialogResult;
import com.ljtxxhwj.myblob.service.ArticleKindService;
import com.ljtxxhwj.myblob.service.ArticleTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ljtxxhwj
 * @since 2024-07-19
 */
@RestController
@RequestMapping("/articleKind")
public class ArticleKindController {
    @Autowired
    private ArticleKindService articleKindService;

    @RequestMapping("/findAllArticleKind.do")
    @ResponseBody
    public List<ArticleKind> findAllArticleKind(){
        return articleKindService.findAllArticleKind();
    }

    @RequestMapping("/findByKindName.do")
    @ResponseBody
    public List<ArticleKind> findByKindName(String kindName){
        return articleKindService.findByKindName(kindName);
    }

    @RequestMapping("/deleteByArticleKindId.do")
    @ResponseBody
    public DialogResult deleteByArticleKindId(Integer articleKindId){
        DialogResult result = new DialogResult();
        try{
            articleKindService.deleteByArticleKindId(articleKindId);
            result.setMsg("删除成功");
        }catch (Exception e){
            result.setMsg("删除失败");
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/saveKind.do")
    @ResponseBody
    public DialogResult saveKind(ArticleKind articleKind){
        DialogResult result = new DialogResult();
        try{
            articleKindService.saveKind(articleKind);
            result.setMsg("新增成功");
        }catch (Exception e){
            result.setMsg("新增失败");
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/deleteBatch.do")
    @ResponseBody
    public DialogResult deleteBatch(String articleKindIds, HttpServletRequest request){
        DialogResult result = new DialogResult();
        String[] ids = articleKindIds.split(",");
        try {
            int num = articleKindService.deleteBatch(ids);
            if(num > 0){
                result.setMsg("删除成功");
            }else {
                result.setMsg("删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("删除失败");
        }
        return result;
    }
}
